package com.sxm.yiwei.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.BuyCarActivity;
import com.sxm.yiwei.beans.Car;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.x;

import java.util.List;

/**
 * Created by KuKa on 2017/7/28.
 * 购物车列表
 */

public class BuyCarAdapter extends BaseAdapter {
    Context context;
    public  List<Car.DataBean> list;
    CarItemOnClick carItemOnClick;
    public BuyCarAdapter(Context context, List<Car.DataBean> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        final ViewHolder holder;
        if (convertView==null){
            view = View.inflate(context, R.layout.buycar_item,null);
            holder = new ViewHolder();
            holder.img = (ImageView) view.findViewById(R.id.car_itemimg);
            holder.name = (TextView) view.findViewById(R.id.car_itemname);
            holder.price = (TextView) view.findViewById(R.id.car_itemprice);
            holder.add = (Button) view.findViewById(R.id.add);
            holder.reduce = (Button) view.findViewById(R.id.reduce);
            holder.result = (TextView) view.findViewById(R.id.t_result);
            holder.tobuy = (Button) view.findViewById(R.id.car_itemtobuy);
            holder.explain = (TextView) view.findViewById(R.id.car_itemexplain);
            holder.delete = (TextView) view.findViewById(R.id.car_itemdelete);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.result.setText("1");
        holder.name.setText(list.get(position).getName());
        holder.explain.setText(list.get(position).getIntro());
       switch (list.get(position).getUnit()){
           case 1:
               holder.price.setText("￥ "+list.get(position).getAmount()+" 元/时");
               break;
           case 2:
               holder.price.setText("￥ "+list.get(position).getAmount()+" 元/天");
               break;
           case 3:
               holder.price.setText("￥ "+list.get(position).getAmount()+" 元/月");
               break;
       }
        x.image().bind(holder.img, InternetAddressUtis.IMG_URL+list.get(position).getServiceAvatar());
        holder.tobuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (carItemOnClick!=null){
                    carItemOnClick.OnClickTobuy(position);
                }
            }
        });
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pp = String.valueOf(holder.result.getText());
                Integer p = Integer.valueOf(pp);
                p++;
                holder.result.setText(p+"");
            }
        });
      holder.reduce.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String dd = String.valueOf(holder.result.getText());
              Integer d = Integer.valueOf(dd);
             if (d>1){
                 d--;
                 holder.result.setText(d+"");
             }else {

             }
          }
      });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (carItemOnClick!=null){
                    carItemOnClick.OnClickDelete(position);
                }
            }
        });
        notifyDataSetChanged();
        return view;
    }

    public interface CarItemOnClick{
        void OnClickTobuy(int p);
        void OnClickDelete(int p);

    }
    public void setCarItemOnClick(CarItemOnClick carItemOnClick){
        this.carItemOnClick=carItemOnClick;
    }

    class ViewHolder{
        ImageView img,head;
        TextView name,price,result,shopname,explain,delete;
        Button add,reduce,tobuy;

    }

}
